/**
 * @param {number[]} nums
 * @return {number[][]}
 */
export var permute = function (nums) {
  const ans = []
  dfs(nums, [], ans)
  return ans
}

function dfs(nums, path, ans) {
  if (path.length === nums.length) {
    ans.push(path.slice())
    return
  }
  for (let i = 0; i < nums.length; i++) {
    const cur = nums[i]
    if (path.includes(cur)) continue
    path.push(cur)
    dfs(nums, path, ans)
    path.pop()
  }
}
